package cn.wolfcode.web.controller;


import cn.wolfcode.common.exception.BusinessException;
import cn.wolfcode.common.web.Result;
import cn.wolfcode.domain.OrderInfo;
import cn.wolfcode.feign.AlipayFeignApi;
import cn.wolfcode.service.IOrderInfoService;
import cn.wolfcode.web.msg.SeckillCodeMsg;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Map;
import java.util.prefs.BackingStoreException;


@RestController
@RequestMapping("/orderPay")
@RefreshScope
public class OrderPayController {
    @Autowired
    private IOrderInfoService orderInfoService;
    @Autowired
    private AlipayFeignApi alipayFeignApi;
    @Value("${pay.frontEndPayUrl}")
    private String frontEndPayUrl;

    @RequestMapping("alipay")
    public Result<String> alipay(String orderNo,int type) {
        // 参数校验
        if (StringUtils.isEmpty(orderNo) || StringUtils.isEmpty(type + "")) {
            throw new BusinessException(SeckillCodeMsg.OPT_ERROR);
        }

        // 判断类型是在线支付还是积分支付
        if (OrderInfo.PAYTYPE_ONLINE.equals(type)) {
            // 走在线支付
            String formString = orderInfoService.payOnline(orderNo, type);
            return Result.success(formString);
        } else if (OrderInfo.PAYTYPE_INTERGRAL.equals(type)) {
            // 走积分支付
            orderInfoService.payIntergral(orderNo,type);
        } else {
            throw new BusinessException(SeckillCodeMsg.OPT_ERROR);
        }
        return Result.success("");
    }

    /**
     * 做数据展示
     * 让他重定向到订单详细页面
     */
    @RequestMapping("return_url")
    public void return_url(@RequestParam Map<String,String> params, HttpServletResponse response) throws IOException {
        Result<Boolean> result = alipayFeignApi.checkSign(params);
        if (result != null && !result.hasError()) {
            if(result.getData()) {
                // 跳转到订单详细页面
                response.sendRedirect(frontEndPayUrl + params.get("out_trade_no"));
            }else {
                // 跳转到错误页面
                response.sendRedirect("http://baidu.com");
            }
        }
    }

    @RequestMapping("notify_url")
    public void notify_url(@RequestParam Map<String,String> params,HttpServletResponse response) throws IOException {
        PrintWriter writer = response.getWriter();
        Result<Boolean> result = alipayFeignApi.checkSign(params);
        if (result != null && !result.hasError()) {
            if (result.getData()) {
                orderInfoService.paySuccess(params);
                writer.write("success");
            } else {
                writer.write("fail");
            }
        }
    }

    @RequestMapping("refund")
    public Result<String> refund(String orderNo) {
        // 参数校验
        if (StringUtils.isEmpty(orderNo)) {
            throw new BusinessException(SeckillCodeMsg.OPT_ERROR);
        }
        // 根据订单编号去redis查询订单信息
        OrderInfo orderInfo = orderInfoService.find(orderNo);
        // 判断支付类型
        if (orderInfo.getPayType().equals(OrderInfo.PAYTYPE_ONLINE)) {
            // 在线退款
            orderInfoService.refundOnline(orderInfo);
        } else if (orderInfo.getPayType().equals(OrderInfo.PAYTYPE_INTERGRAL)) {
            // 积分退款
            orderInfoService.refundIntegral(orderInfo);
        } else {
            throw new BusinessException(SeckillCodeMsg.OPT_ERROR);
        }
        return Result.success("");
    }
}
